package com.codefish.controller;

import java.util.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.codefish.annotation.PassToken;
import com.codefish.dto.AllPermLeverDTO;
import com.codefish.dto.BaseCommonTypeDto;
import com.codefish.entity.SysBaseCommonEntity;
import com.codefish.entity.SysPermissionEntity;
import com.codefish.entity.SysUserTokenEntity;
import com.codefish.service.SysBaseCommonService;
import com.codefish.util.JWTService;
import com.codefish.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;


/**
 * @author codefish
 * @email cool_fish@aliyun.com
 * @date 2021-04-23 11:45:28
 */
@RestController
@RequestMapping("/sysBaseCommon")
public class SysBaseCommonController {

    @Autowired
    private JWTService jwt;

    /**
     * 公共字段
     */
    @Autowired
    private SysBaseCommonService sysBaseCommonService;


    /**
     * 获取公共字段分类
     *
     * @return
     */
    @GetMapping("/getBaseCommonTypeList")
    @ResponseBody
    public R getBaseCommonTypeList() {
        List<BaseCommonTypeDto> reList = new ArrayList<>();

        List<BaseCommonTypeDto> baseCommonTypeList = sysBaseCommonService.getBaseCommonTypeList();

        if (null != baseCommonTypeList && baseCommonTypeList.size() > 0) {
            reList = baseCommonTypeList;
        }

        return R.ok().put("baseCommonTypeList", reList);
    }

    /**
     * 条件分页查询
     *
     * @param params
     * @return
     */
    @GetMapping("/getPageByParams")
    @ResponseBody
    public R getPageByParams(@RequestParam Map<String, Object> params) {


        if (params.get("page") == null || "".equals(params.get("page").toString())) {
            return R.error("参数错误！");
        }
        if (params.get("limit") == null || "".equals(params.get("limit").toString())) {
            return R.error("参数错误！");
        }
        // 条件分页查询
        IPage<SysBaseCommonEntity> entityIPage = sysBaseCommonService.getPageByParams(params);
        List<SysBaseCommonEntity> reList = entityIPage.getRecords();

        long total = entityIPage.getTotal();

        return R.ok().put("entityList", reList).put("total", total);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        SysBaseCommonEntity sysBaseCommon = sysBaseCommonService.getById(id);

        return R.ok().put("sysBaseCommon", sysBaseCommon);
    }

    /**
     * 保存
     */
    @RequestMapping("/saveOne")
    public R saveOne(@RequestBody SysBaseCommonEntity sysBaseCommon) {


        // 查询是否重复
        if (!StringUtils.isEmpty(sysBaseCommon.getBaseCommonKey()) || !StringUtils.isEmpty(sysBaseCommon.getBaseCommonType())) {
            int count = sysBaseCommonService.selectCount(sysBaseCommon);
            if (count > 0) {
                return R.error("公共字段已存在！");
            }
        }

        boolean save = sysBaseCommonService.save(sysBaseCommon);
        if (!save) {
            return R.error("新增失败！");
        }

        return R.ok("新增成功！");
    }

    /**
     * 修改
     */
    @RequestMapping("/updateOne")
    public R updateOne(@RequestBody SysBaseCommonEntity sysBaseCommon) {

        // 先判断名称、分类和启用状态为启用
        int count = sysBaseCommonService.queryCountByNameAndType(sysBaseCommon);
        if (count > 0) {
            //有重复的数据
            return R.error("在" + sysBaseCommon.getTypeName() + "下," + sysBaseCommon.getBaseCommonValue() + "重复");
        }
        sysBaseCommon.setUpdateTime(new Date());
        sysBaseCommonService.updateById(sysBaseCommon);
        return R.ok();
    }

    /**
     * 删除单个公共字段
     *
     * @param id
     * @return
     */
    @GetMapping("/deleteOne")
    public R deleteOne(@RequestParam Long id) {
        boolean deleteFlag = sysBaseCommonService.deleteOne(id);
        if (!deleteFlag) {
            return R.error("删除失败！");
        }

        return R.ok("删除成功！");
    }

    /**
     * 删除
     */
    @GetMapping("/deleteBatch")
    public R deleteBatch(Long[] ids) {
        // 逻辑删除
        sysBaseCommonService.updateStatusAndIsDel(ids);
        return R.ok();
    }

    /**
     * 获取所有权限类型
     *
     * @return
     */
    @GetMapping("/getAllPermLever")
    public R getAllPermLever() {

        List<AllPermLeverDTO> allPermLever = sysBaseCommonService.getAllPermLever();

        if (CollectionUtils.isEmpty(allPermLever)) {
            return R.error("数据为空！");
        }

        return R.ok().put("allPermLever", allPermLever);
    }

    /**
     * 获取所有行业类型
     * @return
     */
    @GetMapping("/getAllCompType")
    public R getAllCompType(){

        List<BaseCommonTypeDto> allCompType = sysBaseCommonService.getAllCompType();

        if (CollectionUtils.isEmpty(allCompType)){
            return R.error("数据为空！");
        }

        return R.ok().put("allCompType",allCompType);
    }

}
